﻿package { 
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.display.Loader;
	import flash.net.URLVariables;
	import flash.net.URLRequestMethod;
	public class RequestSender {
		
		private var loader:URLLoader = null;
		private var callback:Function = null;
		
		public function RequestSender():void {
			
		}
		
		public function load(url:String):void {
			loader = new URLLoader();
			loader.load(new URLRequest(url));
			loader.addEventListener(Event.COMPLETE, onLoaderComlete);
			loader.addEventListener(IOErrorEvent.IO_ERROR, onError);
		}
		
		public function destroy():void {
			if (loader) {
				loader.removeEventListener(Event.COMPLETE, onLoaderComlete);
				loader.removeEventListener(IOErrorEvent.IO_ERROR, onError);
				loader = null;
				callback = null;
			}
		}
		
		public function set onComplete(cb:Function):void{
			callback = cb;
		}
		
		private function onLoaderComlete(event:Event):void{
			loader.removeEventListener(Event.COMPLETE, onLoaderComlete);
			if (loader.data != null) {
				callback(loader.data);
			} 
		}
		
		private function onError(event:Event):void {
			
		}
	}
}
